package com.company.tx2021;

import java.util.Scanner;

public class Main1 {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        ListNode l1 = new ListNode(-1);
        ListNode cur = l1;
        for (int i = 0; i < n; i++) {
            ListNode node = new ListNode(sc.nextInt());
            cur.next = node;
            cur = node;
        }
        int m = sc.nextInt();
        ListNode l2 = new ListNode(-1);
        cur = l2;
        for (int i = 0; i < m; i++) {
            ListNode node = new ListNode(sc.nextInt());
            cur.next = node;
            cur = node;
        }
        ListNode res = getCommonNode(l1,l2);
        print(res);

    }

    private static void print(ListNode node){
        while (node != null){
            System.out.print(node.val);
            if(node.next != null){
                System.out.print(" ");
            }
            node = node.next;
        }
    }

    private static ListNode getCommonNode(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1);
        l1 = l1.next;
        l2 = l2.next;
        ListNode cur = dummy;
        while (l1 != null && l2 != null){
            if(l1.val == l2.val){
                cur.next = l1;
                cur = l1;

                l1 = l1.next;
                l2 = l2.next;
            }else if(l1.val > l2.val){
                l1 = l1.next;
            }else{
                l2 = l2.next;
            }
        }
        cur.next = null;
        return dummy.next;
    }
}

class ListNode{
    int val;
    ListNode next;
    public ListNode(int val){
        this.val = val;
    }
}
